<?php

namespace app\admin\controller;

/**
 * Created by PhpStorm.
 * User: 小狮子
 * Date: 2024/11/5
 * Time: 17:25
 */
use app\Request;

class  Menu
{
    public function menuList(Request $request)
    {
        try {
            $adminUserService = new \app\admin\service\AdminUserService();
            $adminUser = $adminUserService->selectAdminUserRole($request->admin_id);
            if ($adminUser['code'] == 0) {
                return error($adminUser['msg']);
            }
            $roleIds = array_column($adminUser['data'], 'id');

            $url = '/' . $request->rule()->getRule();
            $menu = $adminUserService->getRoleMenu($roleIds, $url);
            if ($menu['code'] == 0) {
                return error($menu['msg']);
            }
            $menuService = new \app\admin\service\MenuService();

            return success('菜单列表',$menuService->buildTree($menu['data']));
        } catch (\think\Exception $exception) {
            return error($exception->getMessage());
        }


    }

    public function addMenu(Request $request)
    {
        $param = $request->param();
        try {
            validate(\app\admin\validate\MenuValidate::class)->check($param);
        } catch (\think\exception\ValidateException $e) {
            return error($e->getError());
        }
        try {
            $menuService = new \app\admin\service\MenuService();
            $menuService->addMenu($param);
            return success('添加成功');
        } catch (\think\exception\ValidateException $e) {
            return error($e->getError());
        }

    }

    public function editMenu(Request $request)
    {
        $param = $request->param();
        try {
            validate(\app\admin\validate\MenuValidate::class)->check($param);
        } catch (\think\exception\ValidateException $e) {
            return error($e->getError());
        }
        try {
            $menuService = new \app\admin\service\MenuService();
            $menuService->editMenu($param);
            return success('修改成功');
        } catch (\think\exception\ValidateException $e) {
            return error($e->getError());
        }

    }

    public function deleteMenu(Request $request)
    {
        $param = $request->param();
        try {
            validate(\app\admin\validate\MenuValidate::class)->check($param);
        } catch (\think\exception\ValidateException $e) {
            return error($e->getError());
        }
        try {
            $menuService = new \app\admin\service\MenuService();
            $menuService->deleteMenu($param);
            return success('修改成功');
        } catch (\think\exception\ValidateException $e) {
            return error($e->getError());
        }

    }



}